#include "work.h"
#include "peripheral.h"
#include "timer.h"
#include "lpuart.h"
#include "flash.h"
#include "temp.h"
#include "basic.h"
#include "lpm.h"
#include "atim3.h"
#include "my_battery.h"


extern uint8_t power_cnt,Current_Temp,flash_cnt1,lv_cnt1,lv_cnt,flash_cnt;
extern uint8_t Work_Mode, Beep_Time;
extern boolean_t flash_f;

boolean_t lvp_f = 0,uvp_f = 0;
static uint8_t charge_delay2 = 0, charge_delay1 = 0, charge_delay = 0;
static uint16_t  chage_full_delay, charge_full_flag;
uint8_t Charge_State = BAT_NOT_CHARG;

float BAT_Voltage = 4.0f;
float bat_buf = 4.0f;
uint8_t stat_cnt = 0, charge_cnt;

#define BAT_MAX_READ_CNT 	20 
uint32_t  bat_adc_buff[BAT_MAX_READ_CNT] ={0};

uint8_t bat_read_cnt;

/********************************
 * 冒泡排序法
*******************************/
static void bubble_sort(uint32_t *arr, uint32_t size)  
{  
    int i, j, tmp;  
    int swap_times = 0;
    
    for (i = 0; i < size - 1; i++) {  
        for (j = 0; j < size - i - 1; j++) {  
            if (arr[j] > arr[j+1]){  
                tmp = arr[j];  
                arr[j] = arr[j+1];  
                arr[j+1] = tmp; 
                swap_times ++; 
            }
        }  
    }  
}  


/***********************************
 * 10ms 执行一次，电池检测
 * 
***************************/
void BAT_Handler(void)	//�����ѹ����
{
	uint32_t AD_Result;

#if 0	// 李茂版本电池检测
	AD_Result = (uint32_t)ADC_Convert(ADC_VBAT_Channel);
	bat_buf = AD_Result*2*3.0f/1024;
	bat_buf = (BAT_Voltage*9+bat_buf)/10.0f;

	if(Charge_State == BAT_NOT_CHARG){
		if(Beep_Time == 0){
			if(BAT_Voltage > bat_buf){
				BAT_Voltage-=0.001f;
			}
		}
		if(BAT_Voltage < 3.4f){
			if(uvp_f == 0){
				uvp_f = 1;
				flash_cnt1 = 0;
				lv_cnt1 = 0;
				lv_cnt = 0;
				flash_cnt = 0;
				flash_f = 0;
			}
		}
		if(BAT_Voltage < 3.5f){
			if(lvp_f==0){
				lvp_f = 1;
				flash_cnt = 0;
				flash_f = 0;
			}
		}
	}
	else {
		if(BAT_Voltage<bat_buf){
			BAT_Voltage += 0.001f;
		}
		
		lvp_f = 0;
		uvp_f = 0;
	}
	// 1秒，电压值更新一次
	if(power_cnt < 100){
		power_cnt++;
		BAT_Voltage = bat_buf;
	}
#endif

	bat_read_cnt ++;
	bat_adc_buff[bat_read_cnt%BAT_MAX_READ_CNT] = (uint32_t)ADC_Convert(ADC_VBAT_Channel);
	if(0 == bat_read_cnt % BAT_MAX_READ_CNT){
		bubble_sort(bat_adc_buff, BAT_MAX_READ_CNT);
		// 取中值
		AD_Result = bat_adc_buff[BAT_MAX_READ_CNT/2];
		// 电压转换
		BAT_Voltage = AD_Result*2*3.0f/1024;
		if(Charge_State == BAT_NOT_CHARG){
			if(BAT_Voltage < 3.3f){
				if(uvp_f == 0){
					uvp_f = 1;
					flash_cnt1 = 0;
					lv_cnt1 = 0;
					lv_cnt = 0;
					flash_cnt = 0;
					flash_f = 0;
				}
			}
			if(BAT_Voltage < 3.4f){
				if(lvp_f==0){
					lvp_f = 1;
					flash_cnt = 0;
					flash_f = 0;
				}
			}
			else if(BAT_Voltage > 3.65f){ // 大于3.65v后将低电标记位清0
				lvp_f = 0;
				uvp_f = 0;
			}
		}
		else {
			lvp_f = 0;
			uvp_f = 0;
		}
	}
}

/***********************************
 * 充电处理函数
 ******************************/ 
void Charge_Handler(void)
{
	if(USB_IN){

		Work_Mode = 0;

		if(stat_cnt < 40){
			// 400ms 消抖
			stat_cnt ++;
		}
		else if(Charge_State == BAT_NOT_CHARG){
			// 从不充电状态，接入到充电状态，先显示充电
			Charge_State = BAT_CHARGING;
			chage_full_delay = 60*1;	// 1min 延时
			charge_full_flag = 0;
		}

		// 1秒时间处理函数
		charge_delay ++;
		if(charge_delay > 100){
			charge_delay = 0;
		
			//4.20V延时10分钟 --缩短为1min
			if(BAT_Voltage > 4.10f){
				if(0== charge_full_flag){

					if(chage_full_delay){
						chage_full_delay --;
					}
					else{
						charge_full_flag = 1;
						Charge_State = BAT_CHARG_FINISH;
					}
				}
			}

		}

		// if(STAT == 0){
		// 	if(Charge_State == 1){
		// 		if(BAT_Voltage > 4.05f){
					
		// 			if(charge_delay > 100){
		// 				charge_delay = 0;
		// 				charge_delay1++;
		// 				if(charge_delay1 > 59){
		// 					charge_delay1=0;
		// 					charge_delay2++;
		// 					//4.05V延时30分钟
		// 					if(charge_delay2 > 29){
		// 						charge_delay2=0;
		// 						Charge_State=2;
		// 					}
		// 				}
		// 			}
		// 		}
		// 	}
		// }
		// else{
		// 	if(stat_cnt>0){
		// 		stat_cnt--;
		// 	}
		// 	else{
		// 		Charge_State=2;
		// 	}	
		// }
	
	}
	else{
		charge_delay = 0;
		charge_delay1 = 0;
		charge_delay2 = 0;
		stat_cnt = 20;
		Charge_State = BAT_NOT_CHARG;
		charge_full_flag = 0;
		chage_full_delay = 0;
	}
}




